/*
 * Copyright (c) 2010, Swedish Institute of Computer Science.
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the name of the Institute nor the names of its contributors
 *    may be used to endorse or promote products derived from this software
 *    without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 *
 * $Id: collect-common.c,v 1.2 2010/11/06 11:55:15 adamdunkels Exp $
 */

/**
 * \file
 *         Example of how the collect primitive works.
 * \author
 *         Adam Dunkels <adam@sics.se>
 */

#include "contiki.h"
#include "net/netstack.h"
#include "dev/serial-line.h"
#include "dev/leds.h"
#include "collect-common.h"
#include "net/rpl/rpl.h"
#include "trickle-ipv6.h"
#include "serial-shell.h"
#include "../../../apps/shell/shell.h"


#include <stdio.h>
#include <ctype.h>

#define DEBUG 0
#if DEBUG
#define PRINTF(...) printf(__VA_ARGS__)
#else
#define PRINTF(...)
#endif

static unsigned long time_offset;

struct etimer send_timer_ipv6, wait_timer, store_timer;
unsigned int transmissionPeriod = 30;
unsigned int randWaitPeriod = 30;
unsigned int samplePeriod = 60;

uint8_t collect_activated = 0;

#define INFINITE_RANK                   0xffff

/*---------------------------------------------------------------------------*/

unsigned char
infinite_dag_rank(void)
{
  rpl_dag_t *dag;
  unsigned char infinite;

  infinite = 1;
  dag = rpl_get_dag(RPL_DEFAULT_INSTANCE);
  if(dag != NULL) {
      PRINTF("Dag_Rank = %u\n", dag->rank);
      if ((dag->rank) != INFINITE_RANK){
          infinite = 0;
      }
  }

  return infinite;
}

void
collect_common_init_shell(void)
{
    serial_shell_init();
    shell_ipv6_start_collect_init();
    shell_ipv6_stop_collect_init();
    shell_ipv6_mac_init();
    shell_ipv6_set_time_init();
    shell_ipv6_net_print_init();
    shell_ipv6_confnodes_init();
}

/*---------------------------------------------------------------------------*/

void
collect_common_start(void)
{
    collect_activated = 1;
}

/*---------------------------------------------------------------------------*/

void
collect_common_stop(void)
{
    collect_activated = 0;
}

/*---------------------------------------------------------------------------*/

unsigned long
get_time(void)
{
  return clock_seconds() + time_offset;
}

void
set_time(unsigned long received_time)
{
  time_offset = received_time - clock_seconds();
}

/*---------------------------------------------------------------------------*/
unsigned long
strtolong(const char *data) {
  unsigned long value = 0;
  int i;
  for(i = 0; i < 11 && isdigit(data[i]); i++) {
    value = value * 10 + data[i] - '0';
  }
  return value;
}

/*---------------------------------------------------------------------------*/
unsigned int
strtoint(const char *data) {
  unsigned int value = 0;
  int i;
  for(i = 0; i < 5 && isdigit(data[i]); i++) {
    value = value * 10 + data[i] - '0';
  }
  return value;
}

/*---------------------------------------------------------------------------*/
void
collect_common_recv(const rimeaddr_t *originator, uint8_t seqno, uint8_t hops,
                    uint32_t time_seconds, uint8_t *payload, uint16_t payload_len)
{
  uint16_t data;
  int i;

  PRINTF("time_seconds = %lu\n", time_seconds);
  printf("%u", 8 + payload_len / 2);
  /* time_seconds is sent by sender nodes */
  printf(" %lu %lu 0", ((time_seconds >> 16) & 0xffff), time_seconds & 0xffff);
  /* Ignore latency for now */
  printf(" %u %u %u %u",
         originator->u8[0] + (originator->u8[1] << 8), seqno, hops, 0);
  for(i = 0; i < payload_len / 2; i++) {
    memcpy(&data, payload, sizeof(data));
    payload += sizeof(data);
    printf(" %u", data);
  }
  printf("\n");
  leds_blink();
}
/*---------------------------------------------------------------------------*/
